package top.shiyiri.datetime;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Aunean
 * @date 2022/1/3
 */
/*
jdk8 中日期时间API的测试
 */
public class JDK8DateTimeTest {

    @Test
    public void testDate() {
        //偏移量
        Date date = new Date(2021 - 1900, Calendar.JANUARY, 3);
        System.out.println(date);//Sun Jan 03 00:00:00 CST 2021
    }

    /*
    LocalDate、LocalTime、LocalDateTime 的使用
     */
    @Test
    public void localTest() {
        //now()：获取当前的日期、时间、日期 + 时间
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);//2022-01-03

        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);//16:52:44.516

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);//2022-01-03T16:52:44.516

        //of()：设置指定的时间。没有偏移量
        LocalDateTime time = LocalDateTime.of(2020, 10, 6, 12, 15);
        System.out.println(time);//2020-10-06T12:15

        //getXxx()
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDate.getDayOfYear());
        System.out.println(localDate.getDayOfYear());

        //体现不可变性
        //withXxx()：设置相关的属性
        LocalDate localDate1 = localDate.withDayOfMonth(12);
        System.out.println(localDate);//2022-01-03
        System.out.println(localDate1);//2022-01-12

        LocalDateTime localDateTime1 = localDateTime.withHour(13);
        System.out.println(localDateTime1);//2022-01-03T13:02:51.496
        System.out.println(localDateTime);//2022-01-03T17:02:51.496

        //不可变性
        LocalDateTime localDateTime2 = localDateTime.plusMonths(3);
        System.out.println(localDateTime2);//2022-04-03T17:04:25.763
        System.out.println(localDateTime);//2022-01-03T17:04:25.763
    }

    /*
    Instant 的使用
    类似于 java.util.Date
     */
    @Test
    public void test2() {
        //now()：获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//2022-01-03T09:18:44.509Z

        //添加时间的偏移量
        OffsetDateTime dateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(dateTime);

        //toEpochMilli()：获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数
        long milli = instant.toEpochMilli();
        System.out.println(milli);//1641201958675

        Instant instant1 = Instant.ofEpochMilli(1641201807377L);
        System.out.println(instant1);//2022-01-03T09:23:27.377Z
    }

    /*
    DateTimeFormatter：格式化或解析日期、时间
    类似于 SimpleDateFormat
     */
    @Test
    public void test3() {
        //方式一：预定义的标准格式。如：ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
        System.out.println(formatter);
        //格式化：日期 -> 字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);//2022-01-03T17:32:11.319
        String format = formatter.format(localDateTime);
        System.out.println(format);//2022-01-03
        //解析：字符串 -> 日期
        TemporalAccessor parse = formatter.parse("2022-01-03");
        System.out.println(parse);//{},ISO resolved to 2022-01-03


        //方式二：本地化相关的格式。
        //如：ofLocalizedDateTime(FormatStyle.LONG)
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        System.out.println(formatter1);//Localized(SHORT,SHORT)
        String format1 = formatter1.format(localDateTime);
        System.out.println(format1);//22-1-3 下午5:46

        //如：ofLocalizedDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        String format2 = formatter2.format(localDateTime);
        System.out.println(format2);//2022-1-3


        //方式三：自定义的格式。如：ofPattern(“yyyy-MM-dd hh:mm:ss”)
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format3 = formatter3.format(localDateTime);
        System.out.println(format3);//2022/01/03
        //解析
        TemporalAccessor parse1 = formatter3.parse("2022-01-03 06:09:18");
        System.out.println(parse1);//{},ISO resolved to 2001-01-03
    }
}
